//
//  XGSinglePictureNewsCell.swift
//  TodayNews
//
//  Created by monkey on 2020/3/26.
//  Copyright © 2020 itcast. All rights reserved.
//

import UIKit

class XGSinglePictureNewsCell: UITableViewCell, XGRegisterReusableViewProtocol, XGNewsProtocol
{
    var newsViewModel: XGNewsViewModel? {
        didSet {
            titleLabel.text = newsViewModel?.title
            infoLabel.text = newsViewModel?.info
            guard let imageUrl = newsViewModel?.middleImage else {
                return
            }
            rightImageView.kf.setImage(with: URL(string: imageUrl))
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?)
    {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        setUpUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    /// 标题
    private lazy var titleLabel = UILabel(text: "测试", textColor: UIColor.black, fontSize: 17, textAlignment: .left)
    /// 描述信息
    private lazy var infoLabel = UILabel(text: "测试", textColor: UIColor.lightGray, fontSize: 13, textAlignment: .left)
    /// 右边图片
    private lazy var rightImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.contentMode = .center
        imageView.clipsToBounds = true
        return imageView
    }()
}

// MARK: - 私有扩展

private extension XGSinglePictureNewsCell
{
    func setUpUI() -> Void
    {
        titleLabel.theme_textColor = "colors.cell_title"
        backgroundColor = UIColor.white
        contentView.addSubview(titleLabel)
        contentView.addSubview(rightImageView)
        contentView.addSubview(infoLabel)
        
        titleLabel.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(10)
            make.right.equalTo(rightImageView.snp.left).offset(-10)
            make.height.lessThanOrEqualTo(80)
        }
    
        rightImageView.snp.makeConstraints { (make) in
            make.top.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-10)
            make.size.equalTo(CGSize(width: 120, height: 80))
        }
        
        infoLabel.snp.makeConstraints { (make) in
            make.top.equalTo(titleLabel.snp.bottom).offset(10)
            make.left.equalToSuperview().offset(10)
            make.right.lessThanOrEqualToSuperview().offset(-10)
        }
    }
}
